/**
 * 
 */
package com.googlecode.stemwijzer.server.session;

import java.util.HashMap;
import java.util.Map;

import com.google.inject.servlet.SessionScoped;
import com.googlecode.stemwijzer.shared.model.IsAnswer;
import com.googlecode.stemwijzer.shared.model.IsQuestion;
import com.googlecode.stemwijzer.shared.model.impl.Question;

/**
 * Bean containing state at the server.
 * 
 * @author roel.hodzelmans
 */
@SessionScoped
public class ServerSession {

	private Map<IsQuestion, IsAnswer> data = new HashMap<IsQuestion, IsAnswer>();
	private String userName = null;

	/**
	 * @param question
	 * @return
	 */
	public IsAnswer getAnswer(final Question question) {
		if (question == null) {
			return null;
		}
		return this.data.get(question);
	}

	/**
	 * @return the userName
	 */
	public String getUserName() {
		return this.userName;
	}

	/**
	 * Resets the current state to null.
	 */
	public void reset() {
		if ((this.userName == null) || this.userName.isEmpty()) {
			this.data = null;
			return;
		}
		this.data = new HashMap<IsQuestion, IsAnswer>();
	}

	/**
	 * @param question
	 * @param answer
	 */
	public void setAnswer(final IsQuestion question, final IsAnswer answer) {
		this.data.put(question, answer);
		System.out.println("Vraag [" + question.toString() + "], antwoord ["
				+ answer.toString() + "]");
	}

	/**
	 * @param userName
	 *            the userName to set
	 */
	public void setUserName(final String userName) {
		this.userName = userName;
	}
}
